<?php


namespace app\api\model;


class Product extends BaseModel
{
    protected $hidden = ['delete_time', 'update_time', 'create_time', 'from', 'pivot', 'category_id'];

    public function imgs()
    {
        return $this->hasMany('ProductImage', 'product_id', 'id');
    }

    public function properties()
    {
        return $this->hasMany('ProductProperty', 'product_id', 'id');
    }

    public function getMainImgUrlAttr($value, $data)
    {
        return $this->prefixImgUrl($value, $data);
    }

    public static function getNewProduct($count)
    {
//        $limit = intval($count) < 15 ? intval($count) : 15;
        return self::limit($count)->order('create_time', 'desc')->select();
    }

    public static function getProductByCategoryID($category_id)
    {
        return self::where('category_id', 'eq', $category_id)->select();
    }

//    获取商品详情
    public static function getProductDetail($id)
    {
        return self::with(['imgs' => function ($query) {
            $query->with('imgUrl')->order('order', 'asc');
        }])->with('properties')->find($id);
    }
}